package io.renren.modules.powercut.service.impl;

import io.renren.modules.powercut.entity.DistrictEntity;
import io.renren.modules.powercut.service.DistrictService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.powercut.dao.RepeatDetailedDao;
import io.renren.modules.powercut.entity.RepeatDetailedEntity;
import io.renren.modules.powercut.service.RepeatDetailedService;


@Service("repeatDetailedService")
public class RepeatDetailedServiceImpl extends ServiceImpl<RepeatDetailedDao, RepeatDetailedEntity> implements RepeatDetailedService {

    @Autowired
    private DistrictService districtService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {


        IPage<RepeatDetailedEntity> page = this.page(
                new Query<RepeatDetailedEntity>().getPage(params),
                getQueryWrapper(params)
        );

        return new PageUtils(page);
    }

    @Override
    public RepeatDetailedEntity queryRepeatDetailedById(String id) {
        RepeatDetailedEntity repeatDetailedEntity = baseMapper.selectRepeatInfoById(id);
        return repeatDetailedEntity;
    }

    private QueryWrapper<RepeatDetailedEntity> getQueryWrapper(Map<String,Object> params){
        System.out.println(params);
        QueryWrapper<RepeatDetailedEntity> repeatDetailedEntityQueryWrapper = new QueryWrapper<>();
        repeatDetailedEntityQueryWrapper.orderByDesc("start_time");
        String company = (String) params.get("company");
        if(StringUtils.isNotEmpty(company)){
            repeatDetailedEntityQueryWrapper.eq("company",company);
        }
        String lineRoadName = (String) params.get("lineRoadName");
        if(StringUtils.isNotEmpty(lineRoadName)){
            repeatDetailedEntityQueryWrapper.eq("line_road_name",lineRoadName);
        }
        String startTime = (String) params.get("startTime");
        if(StringUtils.isNotEmpty(startTime)){
            repeatDetailedEntityQueryWrapper.apply("date_format(start_time,'%Y-%m-%d %H:%i:%S')>=date_format('"+startTime+"','%Y-%m-%d %H:%i:%S')");
        }
        String stopTime = (String) params.get("stopTime");
        if(StringUtils.isNotEmpty(stopTime)){
            repeatDetailedEntityQueryWrapper.apply("date_format('start_time','%Y-%m-%d %H:%i:%S')<=date_format('"+stopTime+"','%Y-%m-%d %H:%i:%S')");
        }
        String manager = (String) params.get("manager");
        System.out.println(manager);
        if(StringUtils.isNotEmpty(manager)){
            QueryWrapper<DistrictEntity> districtEntityQueryWrapper = new QueryWrapper<>();
            districtEntityQueryWrapper.eq("manager",manager);
            districtEntityQueryWrapper.select("user_name");
            List<String> userNames = new ArrayList<>();
            List<DistrictEntity> list = districtService.list(districtEntityQueryWrapper);
            list.parallelStream().forEach(districtEntity -> {
                userNames.add(districtEntity.getUserName());
            });
            repeatDetailedEntityQueryWrapper.in("user_name",userNames);
        }
        String isReporting = (String) params.get("isReporting");
        if(StringUtils.isNotEmpty(isReporting)){
            repeatDetailedEntityQueryWrapper.eq("is_reporting",isReporting);
        }
        String repeatCount = (String) params.get("repeatCount");
        if(StringUtils.isNotEmpty(repeatCount)){
            repeatDetailedEntityQueryWrapper.eq("repeat_count",repeatCount);
        }
        return repeatDetailedEntityQueryWrapper;
    }

}